Skip to content

Python: Improve "bind all interfaces" query#21590

Open
tausbn wants to merge 8 commits intomainfrom
tausbn/python-improve-bind-all-interfaces-query
Open

Python: Improve "bind all interfaces" query#21590
tausbn wants to merge 8 commits intomainfrom
tausbn/python-improve-bind-all-interfaces-query

Conversation

@tausbn
Copy link
Copy Markdown
Contributor

@tausbn tausbn commented Mar 26, 2026

Addresses the false negatives reported in #21582.

Also converts the tests for that query to use inline expectations.

tausbn added 2 commits March 26, 2026 14:56
Adds test cases from #21582 demonstrating false negatives:
- Address stored in class attribute (`self.bind_addr`)
- `os.environ.get` with insecure default value
- `gevent.socket` (alternative socket module)
tausbn added 2 commits March 26, 2026 15:35
This takes care of most of the false negatives from the preceding
commit.

Additionally, we add models for some known wrappers of `socket.socket`
from the `gevent` and `eventlet` packages.
@tausbn tausbn force-pushed the tausbn/python-improve-bind-all-interfaces-query branch from 51859b9 to c0ce669 Compare March 26, 2026 15:36
tausbn added 2 commits March 26, 2026 21:10
Now that we're using global data-flow, we might as well make use of the
fact that we know where the source is.
Looking at the results of the the previous DCA run, there was a bunch of
false positives where `bind` was being used with a `AF_UNIX` socket (a
filesystem path encoded as a string), not a `(host, port)` tuple. These
results should be excluded from the query, as they are not vulnerable.

Ideally, we would just add `.TupleElement[0]` to the MaD sink, except we
don't actually support this in Python MaD...

So, instead I opted for a more low-tech solution: check that the
argument in question flows from a tuple in the local scope.

This eliminates a bunch of false positives on `python/cpython` leaving
behind four true positive results.
@tausbn tausbn marked this pull request as ready for review March 27, 2026 21:22
@tausbn tausbn requested review from a team as code owners March 27, 2026 21:22
Copilot AI review requested due to automatic review settings March 27, 2026 21:22
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Improves the Python py/bind-socket-all-network-interfaces query to reduce false negatives (per #21582) by switching to global data-flow/Models-as-Data, and updates its tests to use inline expectations.

Changes:

  • Convert the query to a path-problem using global taint tracking and Models-as-Data sinks.
  • Add/extend Models-as-Data type/sink models for socket.socket.bind, and wrapper socket implementations in gevent/eventlet.
  • Migrate the query test to inline expectations and add regression cases.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
shared/mad/codeql/mad/ModelValidation.qll Allows the new sink kind used by the query’s Models-as-Data integration.
python/ql/src/Security/CVE-2018-1281/BindToAllInterfaces.ql Updates the query to use global data-flow and emit paths.
python/ql/lib/semmle/python/frameworks/Stdlib.model.yml Adds a bind() sink model and a socket.socket type model entry.
python/ql/lib/semmle/python/frameworks/Gevent.model.yml Models gevent.socket.socket return type as socket.socket.
python/ql/lib/semmle/python/frameworks/Eventlet.model.yml Models eventlet socket wrappers’ return type as socket.socket.
python/ql/test/query-tests/Security/CVE-2018-1281/BindToAllInterfaces_test.py Converts to inline expectations and adds FN regression scenarios.
python/ql/test/query-tests/Security/CVE-2018-1281/BindToAllInterfaces.qlref Enables inline expectation postprocessing for the test.
python/ql/test/query-tests/Security/CVE-2018-1281/BindToAllInterfaces.expected Updates expected output for the new path-problem/inline expectations harness.
python/ql/src/change-notes/2026-03-26-improve-bind-all-interfaces-query.md Documents the analysis change and new library recognition.

tausbn and others added 2 commits March 27, 2026 22:45
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants